package com.spring.web.controller.rest;

import java.util.Date;

import javax.servlet.http.HttpServletRequest;

import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.spring.component.exception.BadRequestException;
import com.spring.model.User;
import com.spring.web.controller.BaseController;

/**
 * Created by harry on 2019/06/20.
 */
@RestController
@RequestMapping("/user")
@Validated
public class UserController extends BaseController {

	@GetMapping(value = "/me", produces = APPLICATION_JSON)
	public ResponseEntity<User> findMe(HttpServletRequest request) {
		// 测试redis Session
		request.getSession().setAttribute("aaa", "bbb");
		return new ResponseEntity<>(new User(1, "lihh", 24, 1, new Date(),null), HttpStatus.OK);
	}

	@GetMapping(value = "/it", produces = APPLICATION_JSON)
	public ResponseEntity<Void> findIt(HttpServletRequest request) {
		// 测试redis Session
		System.out.println(request.getSession().getAttribute("aaa"));
		return new ResponseEntity<>(HttpStatus.OK);

	}
	@GetMapping(value = "/ex/{id}", produces = APPLICATION_JSON)
	public ResponseEntity<Void> ex(@PathVariable(value = "id")int id) {
		// 测试redis Session
//		, HttpServletRequest request
//		System.out.println(request.getSession().getAttribute("aaa"));
		
		if(id < 1) {
			throw new BadRequestException("validate.id.min");
		}
		return new ResponseEntity<>(HttpStatus.OK);

	}

}
